package com.icas.controller;

import com.alibaba.fastjson.JSONObject;
import com.icas.entity.resultentity.Result;
import com.icas.enums.MsgResultEnum;
import com.icas.exception.JsonFormatException;
import com.icas.exception.UpDateException;
import com.icas.service.AttendService;
import com.icas.util.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * 签到表的操作的处理类
 *
 * @Author: tocchen
 * @Date: 2022/4/21 20:02
 * @since jdk 1.8
 **/
@RestController
@RequestMapping("/kingzhihui/attend")
@RequiresAuthentication
public class AttendController {

    @Autowired
    AttendService db;


    static Logger logger = LoggerFactory.getLogger(AttendController.class);

    /**
     * 修改学生在签到表中的状态
     * @param json json--> "签到表id","学生账号" 以及 "修改后的状态"
     * @return 返回处理结果
     */
    @PostMapping("/change/stu_status")
    public Result<?> changeStuAttendStatus(@RequestBody JSONObject json){

        String sheetId = json.getString("sheetId");
        String stuId = json.getString("stuId");
        String status = json.getString("newStatus");

        if (StringUtils.isEmpty(sheetId,stuId,status)){
            logger.warn("Json format Exception");
            throw new JsonFormatException();
        }

        try{
            db.upDateStuStatus(sheetId,stuId,status);
        }catch (Exception e){
            logger.warn("update Exception");
            throw new UpDateException();
        }

        logger.info(stuId+",修改学生在签到表的状态成功");
        return Result.ok();

    }

    /**
     * 修改签到表的状态
     * @param json 带有"sheetId"的json对象
     * @return Result
     */
    @PostMapping("/change/attend_status")
    @RequiresRoles(logical = Logical.OR, value = {"0","1"})
    public Result<?> changeAttendStatus(@RequestBody JSONObject json){

        String sheetId = json.getString("sheetId");

        if (StringUtils.isEmpty(sheetId)){
            logger.warn("Json format Exception");
            throw new JsonFormatException();
        }

        Boolean aBoolean = db.upDateAttendStatus(sheetId);

        return aBoolean?Result.ok():Result.error(MsgResultEnum.ERROR);
    }

    /**
     * 创建签到表
     * @param json 带有 "教室 经度 纬度 课程Id"的json值
     * @return Result
     */
    @PostMapping("/create/attend")
    @RequiresRoles(logical = Logical.OR, value = {"0","1"})
    public Result<?> createAttend(@RequestBody JSONObject json){

        // 教室 经度 纬度 课程Id
        String classRoom = json.getString("classRoom");
        String longitude = json.getString("longitude");
        String latitude = json.getString("latitude");
        String courseId = json.getString("courseId");
        String validTime = json.getString("validTime");
        String range = json.getString("range");

        if (StringUtils.isEmpty(classRoom,longitude,latitude,courseId,validTime,range)){
            logger.warn("Json format Exception");
            throw new JsonFormatException();
        }

        Boolean attend = db.createAttend(courseId,classRoom,longitude,latitude,Long.parseLong(validTime),range);

        return attend?Result.ok():Result.error(MsgResultEnum.ERROR);
    }

}
